// app.js
App({
  onLaunch() {
    // 检查用户是否已经同意隐私协议
    const hasAgreePrivacyAuthorization = wx.getStorageSync('hasAgreePrivacyAuthorization')
    if (!hasAgreePrivacyAuthorization) {
      wx.navigateTo({
        url: '/pages/privacy/privacy'
      })
    }

    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },

  globalData: {
    userInfo: null,
    totalScore: 0  // 添加全局积分
  },

  // 积分规则
  scoreRules: {
    word_challenge: 40,     // 单词闯关总分（20关 × 2分）
    sentence_practice: 100, // 句子练习总分（20关 × 5分）
    invite_friend: 30,      // 邀请好友
    daily_login: 10         // 每日登录
  },

  // 添加积分方法
  addScore(score, type) {
    this.globalData.totalScore += score;
    // 记录积分历史
    const scoreHistory = wx.getStorageSync('scoreHistory') || [];
    scoreHistory.push({
      type: type,
      score: score,
      time: new Date().toISOString()
    });
    wx.setStorageSync('scoreHistory', scoreHistory);
    // 更新存储
    wx.setStorageSync('totalScore', this.globalData.totalScore);
  },

  // 获取总积分
  getTotalScore() {
    const storageScore = wx.getStorageSync('totalScore');
    this.globalData.totalScore = storageScore || 0;
    return this.globalData.totalScore;
  }
})
